#include "userhome.h"
#include "ui_userhome.h"
#include "sqlcontroller.h"
#include "myqmessagebox.h"
#include "maincontroller.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlRelationalDelegate>
#include <QMessageBox>
#include <QSqlRecord>
#include <QLabel>


UserHome::UserHome(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserHome),
    user_id(-1),
    home_id(0),
    home_num(0),
    home_small(nullptr),
    h_find(nullptr)
  , home_mark(0)


{
    ui->setupUi(this);
    this->setWindowTitle("进出货管理界面");

    home_small = new Smallwidget();
    h_find = new HomeFind();

    //视图加入
    auto model = SqlController::getSqlController()->getModel();
    ui->user_tableView->setModel(model);
    ui->user_tableView->verticalHeader()->setHidden(true);//隐藏竖直方向上的自动排序
    ui->user_tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//水平方向自动填充
    ui->user_tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
//    ui->user_tableView->setItemDelegate(new QSqlRelationalDelegate(ui->user_tableView));//委托（方便更改）


    //监听信号用于处理出库入库的操作
    connect(home_small,&Smallwidget::begin,this,[=]()
    {
        if(home_mark == 1)
        {
            home_num = home_num - SqlController::getSqlController()->prodectNum();
            qDebug()<<"user num2 "<< home_num;
            SqlController::getSqlController()->outletnum(home_id,home_num);
            home_small->reset();
            home_small->hide();
            refresh();
            home_mark = -1;
        }
        else if(home_mark ==0)
        {
            home_num = home_num + SqlController::getSqlController()->prodectNum();
            qDebug()<<"user num2 "<< home_num;
            SqlController::getSqlController()->outletnum(home_id,home_num);
            home_small->reset();
            home_small->hide();
            refresh();
            home_mark = -1;
        }
        else
        {
            qDebug()<<"连接错误";
        }

    });

    //查询信号
    connect(h_find,&HomeFind::find,this,[=]()
    {
        qDebug()<<"connect: ";
        ui->user_tableView->setModel(SqlController::getSqlController()->selectProduct());

    });

}

UserHome::~UserHome()
{
    delete ui;
}

//刷新
void UserHome::refresh()
{
    auto model = SqlController::getSqlController()->getModel();
    ui->user_tableView->setModel(model);
}

//出库
void UserHome::on_outlet_btn_clicked()
{
    int row = ui->user_tableView->currentIndex().row();//获取当前选中的行数
    if(row >= 0)
    {
        QSqlRecord record = SqlController::getSqlController()->getModel()->record(row);//通过model将对应的行的记录存放到record中
        home_id = record.value(0).toInt();//获取记录的第0列（id）
        home_num = record.value(2).toInt();//获取记录的第2列（nums）
        home_small->show();//展示控件
        home_mark = 1;

    }
    else
    {
        qDebug()<<"111";
    }



}

//入库
void UserHome::on_import_btn_clicked()
{
    int row = ui->user_tableView->currentIndex().row();//获取当前选中的行数
    if(row >= 0)
    {
        QSqlRecord record = SqlController::getSqlController()->getModel()->record(row);//通过model将对应的行的记录存放到record中
        home_id = record.value(0).toInt();//获取记录的第0列（id）
        home_num = record.value(2).toInt();//获取记录的第2列（nums）
        home_small->show();//展示控件
        home_mark = 0;
    }
    else
    {
        qDebug()<<"import failed";
    }

}




//修改
void UserHome::on_revies_btn_clicked()
{
    int row = ui->user_tableView->currentIndex().row();
    if(row >= 0)
    {
        QSqlRecord record = SqlController::getSqlController()->getModel()->record(row);
        ProductInfo pro;
        pro.id=record.value(0).toInt();
        pro.name=record.value(1).toString();
        pro.nums=record.value(2).toInt();
        pro.classify=record.value(3).toString();
        pro.price=record.value(4).toInt();
        pro.weight=record.value(5).toInt();
        pro.supplier=record.value(6).toString();
        SqlController::getSqlController()->setTempPro(pro);

        //进入修改页面
        MainController::getMainController()->showChangView();

    }
    else
    {
        qDebug()<<"111";
    }
}

void UserHome::on_insert_btn_clicked()
{
    MainController::getMainController()->showinsert();
}

void UserHome::on_flushed_btn_clicked()
{
    refresh();
}


void UserHome::on_quit_btn_clicked()
{
    MainController::getMainController()->showMainWindow();
    MainController::getMainController()->hideUserHome();
}


void UserHome::on_delete_btn_clicked()
{
    int row = ui->user_tableView->currentIndex().row();
    SqlController::getSqlController()->getModel()->removeRow(row);//删除model中的这行记录

    auto ok1 = QMessageBox::warning(this,tr("删除当前行!"), tr("你确定删除当前行吗？"), QMessageBox::Yes, QMessageBox::No);
    if(ok1 == QMessageBox::No)
    { // 如果不删除，则撤销
         SqlController::getSqlController()->getModel()->revertAll();
    }
    else { // 否则提交，在数据库中删除该行
         SqlController::getSqlController()->getModel()->submitAll();
         refresh();
    }

}


void UserHome::on_find_btn_clicked()
{
    h_find->show();
}

